<?php

/**
 * This is the model class for table "tbl_fin_ledger".
 *
 * The followings are the available columns in table 'tbl_fin_ledger':
 * @property string $entryid
 * @property string $transid
 * @property integer $trancount
 * @property string $accountid
 * @property string $amount
 * @property integer $source
 * @property string $transdesc
 * @property string $transmemo
 * @property string $transdate
 * @property string $periodid
 * @property integer $active
 * @property integer $voided
 * @property string $voiddesc
 * @property string $modified
 * @property string $modifiedby
 * @property string $AddedDate
 * @property string $jobid
 * @property string $deptid
 * @property string $jobclassid
 * @property string $itemid
 * @property integer $costtype
 * @property string $units
 * @property string $trannumber
 * @property integer $cleared
 * @property string $docnum
 * @property integer $accrualtype
 * @property string $accruallinkentryid
 * @property string $accrualvendor
 * @property string $duedate
 * @property string $discountdate
 * @property string $discountamount
 * @property string $docaddress
 * @property string $cleareddate
 * @property integer $transtype
 * @property integer $checktietype
 * @property string $checktiecustno
 * @property string $checktieempno
 * @property string $checktievendorno
 * @property string $reference
 * @property string $deliverydate
 * @property string $termid
 * @property string $contact
 * @property string $discounttaken
 * @property integer $billreceived
 * @property string $payitemid
 * @property string $payrate
 * @property string $relatedtransid
 * @property integer $accrualpaidoff
 * @property string $workcompid
 * @property string $workcomprate
 * @property string $payperiodending
 * @property string $taxableamount
 * @property string $payweeks
 * @property string $grosspay
 * @property integer $backloaded
 * @property string $depositcheckno
 * @property string $depositpaymethod
 */
class FinLedger extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return FinLedger the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_fin_ledger';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			//array('entryid, transid, trancount, accountid, amount, source, transdesc, transmemo, periodid, active, voided, voiddesc, modified, modifiedby, jobid, deptid, jobclassid, itemid, costtype, units, trannumber, cleared, docnum, accrualtype, accruallinkentryid, accrualvendor, discountamount, docaddress, cleareddate, transtype, checktietype, checktiecustno, checktieempno, checktievendorno, reference, termid, contact, discounttaken, billreceived, payitemid, payrate, relatedtransid, accrualpaidoff, workcompid, workcomprate, taxableamount, payweeks, grosspay, backloaded, depositcheckno, depositpaymethod', 'required'),
			array('entryid,trancount, active, voided, costtype, cleared, accrualtype, transtype, checktietype, billreceived, accrualpaidoff, backloaded', 'numerical', 'integerOnly'=>true),
			array(' transid, accountid, jobid, deptid, jobclassid, itemid, trannumber, docnum, accruallinkentryid, termid, contact, payitemid, relatedtransid, workcompid', 'length', 'max'=>36),
			array('amount,  units, discountamount, discounttaken, payrate, workcomprate, taxableamount, payweeks, grosspay, depositpaymethod', 'length', 'max'=>10),
			array('transdesc, voiddesc', 'length', 'max'=>150),
			array('periodid', 'length', 'max'=>6),
                        array('source','length','max'=>20),
                        array('modifiedby','length','max'=>30),
			array('accrualvendor, checktieempno, checktievendorno', 'length', 'max'=>4),
			array('cleareddate', 'length', 'max'=>45),
			array('checktiecustno', 'length', 'max'=>7),
			array('reference', 'length', 'max'=>15),
			array('depositcheckno', 'length', 'max'=>12),
                        array('accountid','exist','className'=>'Coa','attributeName'=>'account'),
			array('transdate, AddedDate, duedate, discountdate, deliverydate, payperiodending', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('entryid, transid, trancount, accountid, amount, source, transdesc, transmemo, transdate, periodid, active, voided, voiddesc, modified, modifiedby, AddedDate, jobid, deptid, jobclassid, itemid, costtype, units, trannumber, cleared, docnum, accrualtype, accruallinkentryid, accrualvendor, duedate, discountdate, discountamount, docaddress, cleareddate, transtype, checktietype, checktiecustno, checktieempno, checktievendorno, reference, deliverydate, termid, contact, discounttaken, billreceived, payitemid, payrate, relatedtransid, accrualpaidoff, workcompid, workcomprate, payperiodending, taxableamount, payweeks, grosspay, backloaded, depositcheckno, depositpaymethod', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'entryid' => 'Entryid',
			'transid' => 'Transid',
			'trancount' => 'Trancount',
			'accountid' => 'Accountid',
			'amount' => 'Amount',
			'source' => 'Source',
			'transdesc' => 'Transdesc',
			'transmemo' => 'Transmemo',
			'transdate' => 'Transdate',
			'periodid' => 'Periodid',
			'active' => 'Active',
			'voided' => 'Voided',
			'voiddesc' => 'Voiddesc',
			'modified' => 'Modified',
			'modifiedby' => 'Modifiedby',
			'AddedDate' => 'Added Date',
			'jobid' => 'Jobid',
			'deptid' => 'Deptid',
			'jobclassid' => 'Jobclassid',
			'itemid' => 'Itemid',
			'costtype' => 'Costtype',
			'units' => 'Units',
			'trannumber' => 'Trannumber',
			'cleared' => 'Cleared',
			'docnum' => 'Docnum',
			'accrualtype' => 'Accrualtype',
			'accruallinkentryid' => 'Accruallinkentryid',
			'accrualvendor' => 'Accrualvendor',
			'duedate' => 'Duedate',
			'discountdate' => 'Discountdate',
			'discountamount' => 'Discountamount',
			'docaddress' => 'Docaddress',
			'cleareddate' => 'Cleareddate',
			'transtype' => 'Transtype',
			'checktietype' => 'Checktietype',
			'checktiecustno' => 'Checktiecustno',
			'checktieempno' => 'Checktieempno',
			'checktievendorno' => 'Checktievendorno',
			'reference' => 'Reference',
			'deliverydate' => 'Deliverydate',
			'termid' => 'Termid',
			'contact' => 'Contact',
			'discounttaken' => 'Discounttaken',
			'billreceived' => 'Billreceived',
			'payitemid' => 'Payitemid',
			'payrate' => 'Payrate',
			'relatedtransid' => 'Relatedtransid',
			'accrualpaidoff' => 'Accrualpaidoff',
			'workcompid' => 'Workcompid',
			'workcomprate' => 'Workcomprate',
			'payperiodending' => 'Payperiodending',
			'taxableamount' => 'Taxableamount',
			'payweeks' => 'Payweeks',
			'grosspay' => 'Grosspay',
			'backloaded' => 'Backloaded',
			'depositcheckno' => 'Depositcheckno',
			'depositpaymethod' => 'Depositpaymethod',
		);
	}

    public function getActiveCoaList(){
        $accounts = Coa::model()->findAll(array(
            'condition'=>'inactive = 0',
            'order'=>'account'
        ));
        $list=Yii::app()->db->createCommand('select distinct accountid from tbl_fin_ledger order by accountid')->queryAll();
        
        $data = array(
            ''=>'All'
        );
        foreach($list as $account){
           $data[$account['accountid']] = $account['accountid'].' - '.Coa::model()->getAccountName($account['accountid']);
        }
        return $data;
    }
    public function getAllCoaList(){
        $accounts = Coa::model()->findAll(array(
            'condition'=>'inactive = 0',
            'order'=>'account'
        ));
        $rs=array(
            NULL=>'All'
        );
        foreach($accounts as $item){
            $rs[$item->account]=$item->account.' - '.$item->desc1;
        }
        return $rs;
    }
   
    public function getSourceList(){
        return array(
            
        );
    }
    public function beforeSave() {
        //$coaCheck=Coa::model()->getAccountIncreasesDecreases($this->accountid);
        if($this->isNewRecord){
            $this->AddedDate=DateUtil::getDefaultCurrentDateTime();
        }
        $this->modified=DateUtil::getDefaultCurrentDateTime();
        if(Yii::app()->user->isGuest!=false){
            $this->modifiedby=Yii::app()->user->name;
        }
        
        $coaCheck=Coa::model()->getAccountIncreasesDecreases($this->accountid);
        
        return parent::beforeSave();
    }
    
    public function getAmountType(){
        
        $name=Yii::app()->db->createCommand('select cat.group from tbl_acctcat cat 
               left join tbl_coa coa on coa.id = cat.id
               where coa.account=:account')->bindValue('account', $this->accountid)->queryScalar();
        if($name===false){
            return NULL;
        }
        switch($name){
            case 'L':
               
               if($this->amount<0){
                   return self::DEBIT;
               }elseif($this->amount>0){
                   return self::CREDIT;
               }
               break;
           case 'A':
               
               if($this->amount<0){
                   return self::CREDIT;
               }elseif($this->amount>0){
                   return self::DEBIT;
               }
               break;
           case 'I':
               
               if($this->amount<0){
                   return self::DEBIT;
               }elseif($this->amount>0){
                   return self::CREDIT;
               }
               break;
           case 'E':             
               if($this->amount<0){
                   return self::CREDIT;
               }elseif($this->amount>0){
                   return self::DEBIT;
               }
               break;
        }
        
    }
    const DEBIT=1;
    const CREDIT=2;
}